//
//  NSData+MD5.m
//  emotions
//
//  Created by wd on 15/6/26.
//  Copyright (c) 2015年 wcl. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "NSData+MD5.h"

@implementation NSData (MD5)

+ (NSData *)dataWithUserPath:(NSString *)path
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *dataPath = [documentPath stringByAppendingPathComponent:path];
    NSData *data = [NSData dataWithContentsOfFile:dataPath];
    return data;
}

- (NSString*)MD5
{
    // Create byte array of unsigned chars
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    
    // Create 16 byte MD5 hash value, store in buffer
    CC_MD5(self.bytes, (uint32_t)self.length, md5Buffer);
    
    // Convert unsigned char buffer to NSString of hex values
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 32];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x",md5Buffer[i]];
    
    return output;
}

@end
